package com.xinchao.job.service;

import com.xinchao.common.dto.DeviceSimInfoActivationTimeDTO;
import com.xinchao.common.dto.DeviceSimInfoTrafficDTO;
import com.xinchao.job.mapper.dmp.DeviceSimInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 终端sim卡信息Service
 *
 * @author dxy
 * @date 2018/8/15 10:40
 */
@Service(value = "deviceSimInfoService")
public class DeviceSimInfoService {
	private static Logger logger = LoggerFactory.getLogger(DeviceSimInfoService.class);
	@Autowired
	private DeviceSimInfoMapper deviceSimInfoMapper;
	
	/**
	 * 获取终端sim卡信息总数
	 *
	 * @param city 城市
	 * @return Long
	 */
	public Long countDeviceSimInfo(String city) {
		return deviceSimInfoMapper.countDeviceSimInfo(city);
	}

	/**
	 * 获取第一个终端sim卡信息流量（升序排列）
	 *
	 * @param city 城市
	 * @return DeviceSimInfoTrafficDTO
	 */
	public DeviceSimInfoTrafficDTO selectFirstDeviceSimInfoTraffic(String city) {
		return deviceSimInfoMapper.selectFirstDeviceSimInfoTraffic(city);
	}

	/**
	 * 获取终端流量信息列表
	 *
	 * @param city    城市
	 * @return List<DeviceSimInfoTrafficDTO>
	 */
	public List<DeviceSimInfoTrafficDTO> listDeviceSimInfoTraffic(String city) {
		return deviceSimInfoMapper.listDeviceSimInfoTraffic(city);
	}

	/**
	 * 更新终端sim卡信息当月流量
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceSimInfoMonthTraffic(DeviceSimInfoTrafficDTO dto) {
		deviceSimInfoMapper.updateDeviceSimInfoMonthTraffic(dto);
	}

	/**
	 * 批量更新终端sim卡信息当月流量
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceSimInfoMonthTrafficBatch(List<DeviceSimInfoTrafficDTO> dtoList) {
		deviceSimInfoMapper.updateDeviceSimInfoMonthTrafficBatch(dtoList);
	}

	/**
	 * 获取没有激活时间的终端sim卡信息总数
	 *
	 * @param city 城市
	 * @return Long
	 */
	public Long countDeviceSimInfoNoActivationTime(String city) {
		if (StringUtils.isNotBlank(city)) {
			return deviceSimInfoMapper.countDeviceSimInfoNoActivationTime(city);
		}
		return 0L;
	}

	/**
	 * 获取第一个终端sim卡信息激活时间（升序排列）
	 *
	 * @return DeviceSimInfoTrafficDTO
	 */
	public DeviceSimInfoActivationTimeDTO selectFirstDeviceSimInfoActivationTime(String city) {
		return deviceSimInfoMapper.selectFirstDeviceSimInfoActivationTime(city);
	}

	/**
	 * 获取终端信息激活时间列表
	 *
	 * @param city     城市
	 * @return List<DeviceSimInfoTrafficDTO>
	 */
	public List<DeviceSimInfoActivationTimeDTO> listDeviceSimInfoActivationTime(String city) {
		return deviceSimInfoMapper.listDeviceSimInfoActivationTime(city);
	}

	/**
	 * 更新终端sim卡信息激活时间
	 *
	 * @param dto DeviceSimInfoTrafficDTO
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceSimInfoActivationTime(DeviceSimInfoActivationTimeDTO dto) {
		deviceSimInfoMapper.updateDeviceSimInfoActivationTime(dto);
	}

	/**
	 * 批量更新终端sim卡信息激活时间
	 *
	 * @param dtoList List<DeviceSimInfoActivationTimeDTO>
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceSimInfoActivationTimeBatch(List<DeviceSimInfoActivationTimeDTO> dtoList) {
		deviceSimInfoMapper.updateDeviceSimInfoActivationTimeBatch(dtoList);
	}


	/**
	 * @param dto DeviceSimInfoTrafficDTO
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceSimInfoHistoryTraffic(DeviceSimInfoTrafficDTO dto) {
		deviceSimInfoMapper.updateDeviceSimInfoHistoryTraffic(dto);
	}

	/**
	 * 批量更新终端sim卡信息历史总流量
	 * @param dtoList List<DeviceSimInfoTrafficDTO>
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceSimInfoHistoryTrafficBatch(List<DeviceSimInfoTrafficDTO> dtoList) {
		deviceSimInfoMapper.updateDeviceSimInfoHistoryTrafficBatch(dtoList);
	}
	
	/**
	 * 查询iccid字段为空的记录为其设置sim卡所属城市及iccid
	 * @return
	 */
	public void batchSetSimInfo() {
		int effect = deviceSimInfoMapper.batchUpSimInfo();
		logger.info("== 批量设置iccid和simcity,当前更新条数为：" + effect);
	}
	
}
